package moro;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.*;
import javax.swing.*;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Title:        The MObile RObot Simulation Environment
 * Description:  This window shows what the robot discovers.
 * Copyright:    Copyright (c) 2002
 * Company:      Universit� di Bergamo
 * @author       Davide Brugali
 * @version 1.0
 */

@SuppressWarnings("serial")
public class ControlWindow extends JFrame {
	JPanel contentPane;
        //Een menu bar in het frame
	JMenuBar menuBar = new JMenuBar();
        //Het simulatie menu
	JMenu menuSimulation = new JMenu();
        //Nieuw menu voor de about box
        JMenu menuAbout = new JMenu();
        //Nieuw submenu voor de about box
        JMenuItem menuAboutCredits = new JMenuItem();
        //Submenu's voor het simulatiemenu
	JMenuItem menuSimulationStartWithSonar = new JMenuItem();
	JMenuItem menuSimulationStartWithoutSonar = new JMenuItem();
	JMenuItem menuSimulationQuit = new JMenuItem();
	JMenuItem menuSimulationContinue = new JMenuItem();
	
	JSlider delaySlider = new JSlider(JSlider.HORIZONTAL, 1, 20, 2);

	Controller controller = null;

	public ControlWindow(Controller p_controller) {
		controller = p_controller;
		// See remark in SimulWindow about the statement below
		enableEvents(AWTEvent.WINDOW_EVENT_MASK);
		// ------------------------- contentPane -----------------------------------
		contentPane = (JPanel) this.getContentPane();
		contentPane.setLayout(new BorderLayout());
		this.setTitle("Occupancy Grid");
		// --------------------------- Menu ----------------------------------------
		// Menu Simulation
		menuSimulation.setText("Simulation");
		// Menu Simulation Start met Sonar
		menuSimulationStartWithSonar.setText("Start met Sonar");
		menuSimulationStartWithSonar.addActionListener(new ActionListener()  {
                        @Override
			public void actionPerformed(ActionEvent e) {
				menuSimulationStartWithSonar_actionPerformed(e);
			}
		});
		// Menu Simulation Start zonder Sonar
		menuSimulationStartWithoutSonar.setText("Start zonder Sonar");
		menuSimulationStartWithoutSonar.addActionListener(new ActionListener()  {
                        @Override
			public void actionPerformed(ActionEvent e) {
				menuSimulationStartWithoutSonar_actionPerformed(e);
			}
		});
		// Menu Simulation Quit
		menuSimulationQuit.setText("Robot Pauzeren");
		menuSimulationQuit.addActionListener(new ActionListener()  {
                        @Override
			public void actionPerformed(ActionEvent e) {
				menuSimulationQuit_actionPerformed(e);
			}
		});
		// Menu Simulation Continue
		menuSimulationContinue.setText("Robot Hervatten");
		menuSimulationContinue.addActionListener(new ActionListener()  {
                        @Override
			public void actionPerformed(ActionEvent e) {
				menuSimulationContinue_actionPerformed(e);
			}
		});
		menuSimulation.add(menuSimulationStartWithSonar);
		menuSimulation.add(menuSimulationStartWithoutSonar);
		menuSimulation.add(menuSimulationQuit);
		menuSimulation.add(menuSimulationContinue);
		menuBar.add(menuSimulation);
                //Menu About
                menuAbout.setText("About");
                //Menu Aboutbox
		menuAboutCredits.setText("About the Mobile Robot Explorer");
		menuAboutCredits.addActionListener(new ActionListener()  {
                        @Override
			public void actionPerformed(ActionEvent e) {
				menuAboutCredits_actionPerformed(e);
			}
		});
                menuAbout.add(menuAboutCredits);
                menuBar.add(menuAbout);
                
        /*
         * Setup slider to control speed of simulation
         */
        delaySlider.setToolTipText("Pas snelheid simulatie aan");
        delaySlider.setMajorTickSpacing(1);
        delaySlider.setPaintTicks(true);
        delaySlider.addChangeListener(new ChangeListener() {//Listen to changes on the slider			
			@Override
			public void stateChanged(ChangeEvent e) {//State has changed
				JSlider source = (JSlider)e.getSource();
			    if (!source.getValueIsAdjusting()) {//Slider is now not being adjusted
			        int delay = (int)source.getValue();//Get the value currently set (range goes from 1 till 20)
			        
			        controller.robot.platform.delay = delay;//Set the delay in the platform
					for (Device sensor: controller.robot.sensors)
					{
						sensor.delay = delay;//Set the delay for each of the sensors
					}
			    }
			}
		});
               

		// --------------------------- Panels --------------------------------------
		// draw the OccupancyMap
		this.getContentPane().add(controller.map, BorderLayout.CENTER);
		this.getContentPane().add(delaySlider, BorderLayout.SOUTH);
		this.setJMenuBar(menuBar);
		this.pack();
	}


	// --------------------------- Menu actions ----------------------------------
	/** Menu Simulation Start*/
	void menuSimulationStartWithSonar_actionPerformed(ActionEvent e) {
		controller.start(true);
	}

	/** Menu Simulation Start*/
	void menuSimulationStartWithoutSonar_actionPerformed(ActionEvent e) {
		controller.start(false);
	}
       
	/** Menu Simulation Quit*/
	void menuSimulationQuit_actionPerformed(ActionEvent e) {
		controller.quit();
	}
	/** Menu Simulation Continue*/
	void menuSimulationContinue_actionPerformed(ActionEvent e) {
		controller.continueTask();
	}

        /**Overridden so we can exit when window is closed*/
        @Override
	protected void processWindowEvent(WindowEvent e) {
		super.processWindowEvent(e);
		if (e.getID() == WindowEvent.WINDOW_CLOSING) {
			menuSimulationQuit_actionPerformed(null);
		}
	}
        
        //** Menu About Aboutbox*/
	void menuAboutCredits_actionPerformed(ActionEvent e) {
	        JFrame frame = new JFrame("Credits");         
        //Creates the aboutbox (prototype)
        Container contentPane = frame.getContentPane();
    
        JPanel sPanel = new JPanel();
        sPanel.setLayout(new BoxLayout(sPanel, BoxLayout.X_AXIS));
        sPanel.setPreferredSize(new Dimension(500, 30));
        sPanel.setMaximumSize(new Dimension(500, 30));
        
        JTextArea field = new JTextArea();
        //Tekst voor aboutbox
        field.setText("\nMobile Robot Explorer (c) 2011 \n \n" + "Developed by : \n \n" + "- H. Bleker \n" + "- A. van Zutphen \n" + "- J. Stellingwerf \n" + "- N. Grobben \n");
            
        contentPane.add(sPanel);
        contentPane.add(field);
        frame.pack();
        frame.setLocationRelativeTo(null); //Zet het frame gecentreerd in het scherm.
        frame.setVisible(true);
            	
        }
}
